import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:Java数组相关
 * User: LianBao
 * Date: 2024-02-25
 * Time: 18:27
 */
public class Test {


    public static void main1(String[] args) {
        int[] array = new int[]{1, 2, 3, 4, 5};
        System.out.println(array.length);//数组名.length求得数组长度
        int[] array2 = null;
//        System.out.println(array2.length);抛出空指针异常
        //boolean数组不初始化默认是false
        boolean[] flags = new boolean[2];
        System.out.println(flags[0]);
    }

    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = new int[]{1, 2, 3, 4, 5};
        int[] array3 = new int[5];
        int[] array4 = null;
        //打印数组
        //1.传统的for循环,
        for (int i = 0; i < array1.length; i++) {
            System.out.print(array1[i] + " ");
        }
        System.out.println();

        //2.把数组中每一个数取出来放在x里,得不到下标
        for (int x : array1) {
            System.out.print(x + " ");
        }
        //3.Java自带工具Arrays
        System.out.println(Arrays.toString(array1));

        //Arrays.toString()将数组以字符串形式输出(返回值是String)

        //Arrays.sort();对数组排序,也可以局部排,左闭右开
        Arrays.sort(array1, 1, 3);

        //Arrays.binarySearch()二分查找,也可以限定范围左闭右开
        System.out.println(Arrays.binarySearch(array1, 0, 3, 1));

        //Arrays.copyOf()数组的拷贝

        /**
         * 参数1:代表要拷贝的数组
         * 参数2:代表你要拷贝的数字的长度
         */
        int[] copy = Arrays.copyOf(array2, array2.length);
        int[] copy2 = Arrays.copyOf(array2, array2.length * 2);//可以拷贝+扩容
        /*另一个拷贝的方法
        public static native void arraycopy(Object src,  int  srcPos,Object dest, int destPos,int length);
        * */
        int[] copy3 = Arrays.copyOfRange(array1, 1, 3);//限定范围的拷贝

        //Arrays.equals比较数字是否一样
        System.out.println(Arrays.equals(array1, array2));

        //Arrays.fill 给数组填充
        Arrays.fill(array3, 1, 3, 99);//可以限定范围

        //
        int[][] array6 = {array1, array2};
        int[][] array7 = new int[][]{{1, 2, 3}, {4, 5, 6}};
        int[][] array8 = new int[2][2];
    }

}
